package com.blazingredfireball.rouleurlog.ria.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.ria.business.BikeDelegate;
	import com.blazingredfireball.rouleurlog.ria.event.UpdateBikeEvent;
	import com.blazingredfireball.rouleurlog.ria.model.RouleurlogModelLocator;
	import com.blazingredfireball.rouleurlog.ria.util.ValueObjectUtil;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;


	public class UpdateBikeCommand extends BaseCommand implements ICommand, IResponder
	{
		[Bindable]
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute( event : CairngormEvent ) : void
		{
		    var delegate : BikeDelegate = new BikeDelegate( this );    
		    delegate.updateBike( model.selectedBike.id , UpdateBikeEvent( event ).bike );
		}
		
		public function result( data : Object ) : void
		{
			ValueObjectUtil.updateAttributes( model.selectedBike, data.result );
		}
		
		public function fault( info : Object) : void
		{
			var faultEvent : FaultEvent = FaultEvent( info );
			
		    if (!checkForAuthenticationFault(faultEvent))
		    {
		    	Alert.show("Bike could not be updated!");
		    }
		}
	}
}